package Map;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 编写程序，统计每个字符串 中每个字符出现的次数
 */
public class Test {
    public static void main(String[] args) {
        //
        String line="hhihhhhhhhhuhduhadhskjdajbdshgdhasd";
        Map<Character,Integer> map=new HashMap<>();
        for (int i = 0; i < line.length();i++){
            Character s=line.charAt(i);
            if (map.containsKey(s)){
                map.put(s,map.get(s)+1);//获取当前key对应的value+1
            }else {
                map.put(s,1);//添加新的key-value
            }
        }
        System.out.println(map);
        System.out.println("-----------------------------------");
        //第二种解决思路 双for 循环
        for (int i = 0; i < line.length();i++){
            Character s=line.charAt(i);
            int count=0;
            for (int j = 0; j < line.length();j++){
                if (s==line.charAt(j)){
                    count++;
                }
            }
            map.put(s,count);
        }
        System.out.println(map);
        System.out.println("-------------------------------");
        //第三种解决思路，不使用，map集合



        Set<Character> countedChars = new HashSet<>();
        for (int i = 0; i < line.length();i++){
            char s=line.charAt(i);
            if (!countedChars.contains(s)){
            int count=0;
            for (int j = 0; j < line.length();j++){
                if (s==line.charAt(j)){
                    count++;
                }
            }
            countedChars.add(s);
            System.out.println(s+"出现的次数是"+count);
            }
        }


    }
}
